<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="user_name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户性别</label>
                            <div class="layui-input-inline">
                                <input type="text" name="sex" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">手机号码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="phone" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="CustomerTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">恢复</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">封禁</a>
        </script>

    </div>
</div>
<script src="/static/admin/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/admin/prople/selectAllCustomer',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [

                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'name', width: 110, title: '真实姓名'},
                {field: 'userName', width: 80, title: '登录名'},
                {field: 'phone', width: 80, title: '手机号'},
                {field: 'regTime', width: 160, title: '创建时间',sort: true},
                {field: 'sex', title: '性别', width: 80},
                {title: '状态', width: 80,templet:function(res){
                        if (res.status == 1){
                            return "正常";
                        }else{
                            return "封禁";
                        }
                    }},
                {field: 'integral', width: 80, title: '积分',sort: true},
                {field: 'customerMoney', width: 100, title: '账户余额', sort: true},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]
            ],
            limits: [5, 10,15, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });


        table.on('tool(CustomerTableFilter)', function (obj) {

            var data = obj.data;

            //恢复用户查看用户的状态是否为正常
            var status = data.status;

            if (obj.event === 'edit') {


                layer.confirm('是否要更改员工的状态？', function (index) {

                    if (status == 1){
                        layer.msg("此顾客正常无需恢复")
                    }else{
                        //修改用户的状态
                        $.ajax({
                            url:'/admin/prople/updateCustomerByCustId',
                            type:'post',
                            data:{
                               id:data.id,
                                status:1
                            },
                            dataType:'json',
                            success:function (res) {
                                layer.msg(res.message)
                                //刷新表格
                                table.reload('currentTableId', {
                                    url: '/admin/prople/selectAllCustomer'

                                });
                            }
                        })
                    }

                });

            } else if (obj.event === 'delete') {

                layer.confirm('是否要更改顾客的状态？', function (index) {

                    if (status == 0){
                        layer.msg("此顾客已被封禁")
                    }else{
                        //修改用户的状态
                        $.ajax({
                            url:'/admin/prople/updateCustomerByCustId',
                            type:'post',
                            data:{
                                id:data.id,
                                status:0
                            },
                            dataType:'json',
                            success:function (res) {
                                layer.msg(res.message)

                                //刷新表格
                                table.reload('currentTableId', {
                                    url: '/admin/prople/selectAllCustomer'

                                });

                            }
                        })
                    }
                });
            }
        });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (obj) {
            var data = obj.field;
            console.log(obj)
            if(data.user_name == null && data.sex == null && data.phone == null){
                layer.msg("请至少选择一个查询")
            }else{

                //执行搜索重载
                table.reload('currentTableId', {
                    where: {

                        userName:data.user_name,
                        sex:data.sex,
                        phone:data.phone

                    },
                    url:'/admin/prople/queryCustomerByNameOrSexOrPhoneOrStatus'

                });

            }
            return false;
        });




    });


</script>

</body>
</html>
